package TableAdditions;
import java.awt.EventQueue;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import javax.swing.JTextField;

import Tables.CustomerTableInterface;
import Utility.JTextFieldFilter;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import java.awt.Font;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class CustomerTableAddition extends JFrame{


	
	    private JTextField customerCodeField;
	    private JTextField customerAuthorityField;
	    private JTextField customerNameField;
	    private JTextField customerPhoneField;
	    private JTextField customerAddress1Field;
	    private JTextField customerAddress2Field;
	    private JTextField customerTownField;
	    private JTextField customerCityField;

	    
		public String customerCode;
		public String customerName;
		public String customerAuthority;
		public String customerPhone;
		public String customerAddress1;
		public String customerAddress2;
		public String customerTown;
		public String customerCity;
	    
	    private JButton customerAddButton;
	    
	    
	    CustomerTableInterface cti;
	    
	    public CustomerTableAddition(final CustomerTableInterface cti) {
			super("Müşteri Ekleme Ekranı");
			this.cti = cti;
			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			setSize(600,200);
			getContentPane().setLayout(null);
			
			JLabel codeLabel = new JLabel("Müşteri Kodu");
			codeLabel.setBounds(20, 10, 115, 20);
			getContentPane().add(codeLabel);
			
			customerCodeField = new JTextField();
			customerCodeField.setBounds(130, 10, 115, 20);
			getContentPane().add(customerCodeField);
			customerCodeField.setColumns(10);
			
			JLabel authorityLabel = new JLabel("Yetkili Kişi");
			authorityLabel.setBounds(20, 40, 90, 20);
			getContentPane().add(authorityLabel);
			
			customerAuthorityField = new JTextField();
			customerAuthorityField.setBounds(130, 40, 115, 20);
			getContentPane().add(customerAuthorityField);
			customerAuthorityField.setColumns(10);
			
			JLabel PhoneLabel = new JLabel("Telefon");
			PhoneLabel.setBounds(270, 40, 90, 20);
			getContentPane().add(PhoneLabel);
			
			JLabel nameLabel = new JLabel("Müşteri Adı");
			nameLabel.setBounds(270, 10, 90, 20);
			getContentPane().add(nameLabel);
			
			customerNameField = new JTextField();
			customerNameField.setBounds(370, 10, 115, 20);
			getContentPane().add(customerNameField);
			customerNameField.setColumns(10);
			
			customerPhoneField = new JTextField();
			customerPhoneField.setBounds(370, 40, 115, 20);
			getContentPane().add(customerPhoneField);
			customerPhoneField.setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
			customerPhoneField.setColumns(10);
			
			JLabel address1Label = new JLabel("Adres 1");
			address1Label.setBounds(20, 70, 90, 20);
			getContentPane().add(address1Label);
			
			customerAddress1Field = new JTextField();
			customerAddress1Field.setBounds(130, 70, 115, 20);
			getContentPane().add(customerAddress1Field);
			customerAddress1Field.setColumns(10);
			
			JLabel adress2Label = new JLabel("Adres 2");
			adress2Label.setBounds(270, 70, 90, 20);
			getContentPane().add(adress2Label);
			
			customerAddress2Field = new JTextField();
			customerAddress2Field.setBounds(370, 70, 115, 20);
			getContentPane().add(customerAddress2Field);
			customerAddress2Field.setColumns(10);
			
			JLabel townLabel = new JLabel("İlçe");
			townLabel.setBounds(20, 100, 90, 20);
			getContentPane().add(townLabel);
			
			customerTownField = new JTextField();
			customerTownField.setBounds(130, 100, 115, 20);
			getContentPane().add(customerTownField);
			customerTownField.setColumns(10);
			
			JLabel cityLabel = new JLabel("Şehir");
			cityLabel.setBounds(270, 100, 90, 20);
			getContentPane().add(cityLabel);
			
			customerCityField = new JTextField();
			customerCityField.setBounds(370, 100, 115, 20);
			getContentPane().add(customerCityField);
			customerCityField.setColumns(10);
			
			
			customerAddButton = new JButton("Ekle");
			customerAddButton.setForeground(Color.BLACK);
			//customerAddButton.setBounds(370, 120, 115, 20);
			customerAddButton.setFont(new Font("Arial", Font.BOLD, 16));
			customerAddButton.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
				    
					
					
					customerCode=customerCodeField.getText();
					customerName=customerNameField.getText();
					customerAuthority=customerAuthorityField.getText();
					customerPhone=customerPhoneField.getText();
					customerAddress1=customerAddress1Field.getText();
					customerAddress2=customerAddress2Field.getText();
					customerTown=customerTownField.getText();
					customerCity=customerCityField.getText();

					String insert="INSERT INTO customer (customer_code,customer_name,customer_authority,customer_phone,customer_address," +
				            		"customer_address2,customer_town,customer_city)";
					String values="VALUES ('"+customerCode+"'" +
					",'"+customerName+"','"+customerAuthority+"','"+customerPhone+"','"+customerAddress1+"','"+customerAddress2+"','"+customerTown+"','"+customerCity+"')";
					Object[][] newRow = {
					        {customerCode, customerName,customerAuthority,customerPhone, customerAddress1,
					       customerAddress2,customerTown,customerCity}};
					
					System.out.println("Ready to execute sql");
					 	Connection con = null;
				        Statement st = null;
				        ResultSet rs = null;

				        String url = "jdbc:mysql://localhost:3306/mydb";
				        String user = "root";
				        String password = "qweasdzxc";

				        try {
				            con = (Connection) DriverManager.getConnection(url, user, password);
				            st = (Statement) con.createStatement();
				            //st.executeUpdate("INSERT INTO job " + "VALUES (1, 'deneme')");
				         //   st.executeUpdate("UPDATE customer SET customer_phone='0554755' WHERE customer_name='rehber';");
				            st.executeUpdate(insert + values);
				            //addTa(newRow[0]);
				            //st.executeUpdate("DELETE FROM job");
				          
				            int messageType = JOptionPane.PLAIN_MESSAGE;
				          
				            JOptionPane.showMessageDialog(null, "Kayıt Alınmıştır", "Uyarı!!", messageType);
				            con.close();
				        } catch (SQLException ex) {
				            Logger lgr = Logger.getLogger(main.class.getName());
				            lgr.log(Level.SEVERE, ex.getMessage(), ex);

				        } finally {
				            try {
				                if (rs != null) {
				                    rs.close();
				                }
				                if (st != null) {
				                    st.close();
				                }
				                if (con != null) {
				                    con.close();
				                }

				            } catch (SQLException ex) {
				                Logger lgr = Logger.getLogger(main.class.getName());
				                lgr.log(Level.WARNING, ex.getMessage(), ex);
				            }
				        }
				        
				        // this will make sure WindowListener.windowClosing() et al. will be called.
				      //  WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
				        //Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);

				        // this will hide and dispose the frame, so that the application quits by
				        // itself if there is nothing else around. 
				        cti.addNewItem();
				        setVisible(false);
				        dispose();
				}});
			
			customerAddButton.setBounds(380, 130, 100, 25);
			getContentPane().add(customerAddButton);
			
			
		setVisible(true);  
		}
	    
		
}
